package com.uchiha.nova.mainService.wrapper;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.uchiha.nova.core.model.entity.NavMenuEntity;
import com.uchiha.nova.core.model.vo.NavMenuVO;
import com.uchiha.nova.core.tool.node.ForestNodeMerger;
import com.uchiha.nova.core.tool.support.BaseEntityWrapper;

import com.uchiha.nova.core.tool.util.SpringUtil;
import com.uchiha.nova.mainService.service.INavMenuService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 侧边栏 包装类,返回视图层所需的字段
 *
 * @author Chill
 */
public class NavMenuWrapper extends BaseEntityWrapper<NavMenuEntity, NavMenuVO>  {

	INavMenuService navMenuService = SpringUtil.getBean(INavMenuService.class);

	public static NavMenuWrapper build() {
		return new NavMenuWrapper();
 	}

	@Override
	public NavMenuVO entityVO(NavMenuEntity navMenuEntity) {
		NavMenuVO navMenuVO = BeanUtil.copyProperties(navMenuEntity, NavMenuVO.class);
		Long a = navMenuService.count(new QueryWrapper<NavMenuEntity>().eq("parent_id",navMenuEntity.getId()));
		if(a.compareTo(0L)==1){
			navMenuVO.setHasChildren(true);
		}else{
			navMenuVO.setHasChildren(false);
		}
		if(navMenuEntity.getParentId().compareTo(0L)!=0){
			NavMenuEntity navMenuParent = navMenuService.getById(navMenuEntity.getParentId());
			navMenuVO.setParentName(navMenuParent.getLabel());
		}else{
			navMenuVO.setParentName("顶级菜单");
		}
		return navMenuVO;
	}

	public List<NavMenuVO> listNodeVO(List<NavMenuEntity> list) {
		List<NavMenuVO> collect = list.stream().map(navMenuEntity -> {
			NavMenuVO navMenuVO = BeanUtil.copyProperties(navMenuEntity, NavMenuVO.class);
			Long a = navMenuService.count(new QueryWrapper<NavMenuEntity>().eq("parent_id",navMenuEntity.getId()));
			if(a.compareTo(0L)==1){
				navMenuVO.setHasChildren(true);
			}else{
				navMenuVO.setHasChildren(false);
			}
			return navMenuVO;
		}).collect(Collectors.toList());
		return ForestNodeMerger.merge(collect);
	}

	@Override
	public IPage<NavMenuVO> pageVO(IPage<NavMenuEntity> pages) {
		IPage<NavMenuVO> page = pages.convert(item->{
			NavMenuVO navMenuVO = BeanUtil.copyProperties(item, NavMenuVO.class);
			Long a = navMenuService.count(new QueryWrapper<NavMenuEntity>().eq("parent_id",item.getId()));
			if(a.compareTo(0L)==1){
				navMenuVO.setHasChildren(true);
			}else{
				navMenuVO.setHasChildren(false);
			}
			return navMenuVO;
		});
		return page;
	}

}
